package com.study.springsecurity.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.Claims;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import org.springframework.security.core.userdetails.UserDetails;


import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;

/**
 * JWT工具类
 */
public class JwtUtil {

    //有效期为
    public static final Long JWT_TTL = 60 * 60 *1000L;// 60 * 60 *1000  一个小时
    //设置秘钥明文
    public static final String JWT_KEY = "sangeng";

    public static String getUUID(){
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        return token;
    }

    /**
     * 生成jtw
     * @param userDetails token中要存放的数据
     * @return
     */
    public static String createJWT(UserDetails userDetails) {
        DateTime now = DateTime.now();
        DateTime newTime = now.offsetNew(DateField.HOUR, 48);

        Map<String,Object> payload = new HashMap<String,Object>();
        //签发时间
        payload.put(JWTPayload.ISSUED_AT, now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, newTime);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);
        //载荷
        payload.put("username", userDetails.getUsername());
        payload.put("password", userDetails.getPassword());

        return JWTUtil.createToken(payload, JWT_KEY.getBytes());
    }

    /**
     * 生成jtw
     * @param userDetails token中要存放的数据
     * @param ttlMillis token超时时间
     * @return
     */
    public static String createJWT(UserDetails userDetails, int ttlMillis) {
        DateTime now = DateTime.now();
        DateTime newTime = now.offsetNew(DateField.MINUTE, ttlMillis);
        Map<String,Object> payload = new HashMap<String,Object>();
        //签发时间
        payload.put(JWTPayload.ISSUED_AT, now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, newTime);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);
        //载荷
        payload.put("username", userDetails.getUsername());
        payload.put("password", userDetails.getPassword());
        return JWTUtil.createToken(payload, JWT_KEY.getBytes());
    }


    /**
     * 生成加密后的秘钥 secretKey
     * @return
     */
    public static SecretKey generalKey() {
        byte[] encodedKey = Base64.getDecoder().decode(JWT_KEY);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }

    /**
     * 解析
     *
     * @param token : 生成的token
     * @return token解析claim
     */
    public static Claims parseJWT(String token)  {
        // 使用Hutool解析JWT
        JWT jwt = JWTUtil.parseToken(token);
        System.out.println("JWT Payload: " + jwt.getPayload());
        return new Claims();
    }

    /**
     * 验证是否是一个密钥合法token
     * @param token 生成的token
     * @return 返回是否合法
     */
    public static boolean verifyKey (String token) {
        JWT jwt = JWTUtil.parseToken(token);
        return jwt.setKey(JWT_KEY.getBytes()).verify();
    }
    /**
     * 验证是否是一个未过期token
     * @param token 生成的token
     * @return 返回是否合法
     */
    public static boolean verifyTime (String token) {
        JWT jwt = JWTUtil.parseToken(token);
        return jwt.validate(0);
    }

    /**
     * 验证是否是一个合法token
     * @param token 生成的token
     * @return 返回是否合法
     */
    public static boolean verify(String token) {
        return verifyKey(token)&&verifyTime(token);
    }

    public static void main(String[] args) throws Exception {
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjYWM2ZDVhZi1mNjVlLTQ0MDAtYjcxMi0zYWEwOGIyOTIwYjQiLCJzdWIiOiJzZyIsImlzcyI6InNnIiwiaWF0IjoxNjM4MTA2NzEyLCJleHAiOjE2MzgxMTAzMTJ9.JVsSbkP94wuczb4QryQbAke3ysBDIL5ou8fWsbt_ebg";
        Claims claims = parseJWT(token);
        System.out.println(claims);
    }

}
